package com.example.dishit.view.friendmaint;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.dishit.R;
import com.example.dishit.controller.friendmaint.FriendMaintController;

public class FriendArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final List<String> dishers;
    
    public FriendArrayAdapter(Context context, List<String> dishers) {
        super(context, R.layout.disher_data, dishers);
        this.context = context;
        this.dishers = dishers;
    }
    
    @Override
    public View getView(int position, final View convertView, final ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View disherView = inflater.inflate(R.layout.dishdata, parent, false);
        
        // TEXT
        final TextView textView = (TextView) disherView.findViewById(R.id.text);
        final String disher = dishers.get(position);
        textView.setText(disher);
        final int originalColor = textView.getTextColors().getDefaultColor();
        
        // Add click-listener for the row.
        disherView.setOnClickListener(new OnClickListener() {
            private boolean isSelected = false;
            
            public void onClick(View arg0) {
                isSelected = !isSelected;
                
                if(isSelected) {
                    textView.setTextColor(Color.YELLOW);
                    FriendMaintController.instance().addSelection(disher);
                } else {
                    textView.setTextColor(originalColor);
                    FriendMaintController.instance().removeSelection(disher);
                }
            }
        });
        
        
        return disherView;
    }
}
